<?php $this->load->view('helpers/header.php'); ?>
<?php $this->load->view($module . '/' . $view_file);?>
<?php $this->load->view('helpers/footer.php'); ?>
<script type="text/javascript">
/* Property Classification */
$(document).ready(function() {
	function getURLVar(key) {
		var value = [];
		var query = String(document.location).split('?');
		
		if (query[1]) {
			var part = query[1].split('&');

			for (i = 0; i < part.length; i++) {
				var data = part[i].split('=');
				
				if (data[0] && data[1]) {
					value[data[0]] = data[1];
				}
			}
			
			if (value[key]) {
				return value[key];
			} else {
				return '';
			}
		}
	}
	
	$('#ddlPropertyClss').bind('change', function() {
		$.ajax({
			url: siteurl + 'member/getAllPropertiesByType?property_clss_type=' + this.value,
			dataType: 'json',
			beforeSend: function() {
				$('select[name=\'ddlPropertyClss\']').after('<span class="wait">&nbsp;<img src="'+ siteurl +'assets/img/loading.gif" alt="" /></span>');
			},
			complete: function() {
				$('.wait').remove();
			},			
			success: function(json) {
				var ddlPropertyType = getURLVar('ddlPropertyType');
				html = '<option value=""><?php echo $text_all; ?></option>';
				
				if (json.length > 0) {
					for (i = 0; i < json.length; i++) {
						
	        			html += '<option value="' + json[i]['PropertyType_ID'] + '"';

	        			if(ddlPropertyType != '' && ddlPropertyType == json[i]['PropertyType_ID']) {
							html += 'selected="selected"';
		        		}
		    			
						html += '>' + json[i]['Name'] + '</option>';
					}
				}
				
				$('select[name=\'propertytype\']').html(html);
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	});

	$('#ddlPropertyClss').trigger('change');

	//Event click on Advanced button
	$('.btn-advanced-search').click(function(){
		window.location.href = siteurl + 'advancedsearch';
	});


	function sbShowLocationArea(b) {
	    $("."+b).css("display","block");
	}

	function sbHideLocationArea(b) {
	    $("."+b).css("display","none");
	}

	//Show city frame
	$('span[id=\'a_search_city_area_advanced_selections\']').bind('click', function() {
		$('.search_cityareapanel').show();
	});

	//Hide city frame
	$('.city_advance_search').mouseleave(function(){
		$('.search_cityareapanel').hide();
	});

	//Show location frame
	$('span[id=\'a_search_location_area_advanced_selections\']').bind('click', function() {
		cityArr = new Array;
		locationArr = new Array;
		
		cityArr = $('#ddlCity input:checkbox:checked').map(function() {
            return this.value;
        }).get().join(',');

		locationArr = $('#ddlLocation input:checkbox:checked').map(function() {
            return this.value;
        }).get().join(',');

		if(cityArr.length <= 0) {
			$('.search_locationareapanel').hide();
			alert('<?php echo 'Please choose city first'; ?>');
		} else {
			$('.search_locationareapanel').show();
		}
	});

	//Hide city frame
	$('.location_advance_search').mouseleave(function(){
		$('.search_locationareapanel').hide();
	});
	

	//Show sub area frame
	$('span[id=\'a_search_subarea_area_advanced_selections\']').bind('click', function() {
		cityArr = new Array;
		locationArr = new Array;
		
		cityArr = $('#ddlCity input:checkbox:checked').map(function() {
            return this.value;
        }).get().join(',');

		locationArr = $('#ddlLocation input:checkbox:checked').map(function() {
            return this.value;
        }).get().join(',');

		if(cityArr.length <= 0) {
			alert('<?php echo 'Please choose city and location first'; ?>');
			$('.search_subareaareapanel').hide();	
		} else {
			$('.search_subareaareapanel').show();
		}
	});

	//Hide city frame
	$('.subarea_advance_search').mouseleave(function(){
		$('.search_subareaareapanel').hide();
	});

	//Get list all location based on an array of city id
	/*
	$('#ddlCity').delegate('input:checkbox', 'click', function(){
		alert("here");
		//city
        var cities = new Array;
        cities = $('#ddlCity input:checkbox:checked').map(function() {
            return this.value;
        });

        for(var i = 0, l = cities.length; i < l; i++) {
            alert(cities[i]);
        }
	});
	*/

	function hideZone(b) {
	   $("."+b).css("display","none");
	}

	/*Check (find) an item in Javascrip Array
     */
    Array.prototype.has=function(v){
        for (i=0;i<this.length;i++){
            if (this[i] == v) 
                return true;
        }
        return false;
    };

    function in_array (needle, haystack, argStrict) {
      // http://kevin.vanzonneveld.net
      // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
      // +   improved by: vlado houba
      // +   input by: Billy
      // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
      // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
      // *     returns 1: true
      // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
      // *     returns 2: false
      // *     example 3: in_array(1, ['1', '2', '3']);
      // *     returns 3: true
      // *     example 3: in_array(1, ['1', '2', '3'], false);
      // *     returns 3: true
      // *     example 4: in_array(1, ['1', '2', '3'], true);
      // *     returns 4: false
      var key = '',
        strict = !! argStrict;

      if (strict) {
        for (key in haystack) {
          if (haystack[key] === needle) {
            return true;
          }
        }
      } else {
        for (key in haystack) {
          if (haystack[key] == needle) {
            return true;
          }
        }
      }

      return false;
    }

  //determine whether object is empty or not
    function isEmpty(b) {
        if(b.length > 0) {
			return true;
        }

        return false;
    }

  //count item in array
    function countItemNotIsEmpty(b) {
        var count = 0;
        for(var i = 0; i < b.length; i++) {
            if(b[i] != "") {
                count++;
            }
        }
        return count;
    }

	<?php if($this->uri->segment(1) == 'listing-rows' || $this->uri->segment(1) == 'listing-grid') { ?>
	
	//first time loading
	cityArr = $('#ddlCity .child_city input:checkbox:checked').map(function() {
        return this.value;
    }).get().join(',');

	var locationArr = "";
    <?php if(is_array($locations)) { ?>
		locationArr = '<?php echo implode(',', $locations); ?>';
	<?php } ?>

	if(cityArr.indexOf(',') == 0) {
    	cityArr = cityArr.replace(',','');
    }

    if(locationArr.length > 0) {
    	locationArr = locationArr.split(',');
    }

    if(cityArr.length > 0) {
        $.ajax({
			url: 'member/getListAllLocation?citiesVal=' + cityArr,
			dataType: 'json',
			beforeSend: function() {
				$('#ddlLocation').after('<span class="wait">&nbsp;<img src="'+ siteurl +'assets/img/loading.gif" alt="" /></span>');
			},
			complete: function() {
				$('.wait').remove();
			},			
			success: function(json) {
				var html = "";
				$.each(json, function(entryIndex, entry){
					if (entry['zone'] != '') {
						html += '<div class="span12">';
						html += '<label>';
						html += ''+ entry['Name'] +'';
						html += '</label>';
						html += '<label for="all_location" class="all_location">';
						html += '<input type=\'checkbox\' value checked="checked" class="formcheck childCheckBoxForLocaion" onclick="sbRestArea(\'search_location_area_selections2\',\'childCheckBoxForLocaion\',\'<?php echo $text_location_selected; ?>\')">';
						html += '<span style="cursor:default;" onclick="sbRestArea(\'search_location_area_selections2\',\'childCheckBoxForLocaion\',\'<?php echo $text_location_selected; ?>\')">';
						html += '<?php echo $text_all; ?>';
						html += '</span>';
						html += '</label>';

						for (i = 0; i < entry['zone'].length; i++) {
							html += '<label for="location_'+ entry['zone'][i]['Location_ID'] +'" class="child_location">';
							if(in_array(entry['zone'][i]['Location_ID'],locationArr)) {
								html += '<input type=\'checkbox\' value="' + entry['zone'][i]['Location_ID'] + '" checked="checked" class="formcheck childCheckBoxForLocaion" id="sbChksL'+ entry['zone'][i]['Location_ID'] +'" name="locations[]" onclick="sbPopulateArea(\'search_location_area_selections2\',\'childCheckBoxForLocaion\',\'<?php echo $text_location_selected; ?>\')">';
							}else {
								html += '<input type=\'checkbox\' value="' + entry['zone'][i]['Location_ID'] + '" class="formcheck childCheckBoxForLocaion" id="sbChksL'+ entry['zone'][i]['Location_ID'] +'" name="locations[]" onclick="sbPopulateArea(\'search_location_area_selections2\',\'childCheckBoxForLocaion\',\'<?php echo $text_location_selected; ?>\')">';
							}
							html += '<span style="cursor:default;" class="sbChks" onclick="sbCheckUncheck(\'sbChksL'+entry['zone'][i]['Location_ID']+'\', \'search_location_area_selections2\',\'childCheckBoxForLocaion\',\'<?php echo $text_location_selected; ?>\')">';
							html += ''+ entry['zone'][i]['Name'] +'';
							html += '</span>';
							html += '</label>';
						}
						
						html += '</div>';
					} else {
						//case exception: when Bali is being selected
						//process later for this case
						$('#ddlLocation').empty();
					}
					
					$('#ddlLocation').html(html);
				});
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
    } else {
        //reset all selected input tag
    	$('#ddlLocation input:checkbox:checked').attr('checked', false);
    	$('#ddlSubArea input:checkbox:checked').attr('checked', false);
    	//hide location frame
    	hideZone("search_locationareapanel");
    	hideZone("search_subareaareapanel");
    	$('#ddlLocation .span12').remove();
    }
    
	<?php } ?>
    
    
    
	
	$('#ddlCity input').bind('click', function(){
		var cityArr = new Array;
		var locationArr = new Array;
		
        cityArr = $('#ddlCity .child_city input:checkbox:checked').map(function() {
            return this.value;
        }).get().join(',');

        locationArr = $('#ddlLocation input:checkbox:checked').map(function() {
            return this.value;
        }).get().join(',');

        if(cityArr.indexOf(',') == 0) {
        	cityArr = cityArr.replace(',','');
        }

        if(locationArr.indexOf(',') == 0) {
        	locationArr = locationArr.replace(',','');
        }

	    //case 1: both cities and location are selected

        //case 2: only select cities
        if(cityArr.length > 0) {
	        $.ajax({
				url: 'member/getListAllLocation?citiesVal=' + cityArr,
				dataType: 'json',
				beforeSend: function() {
					$('#ddlLocation').after('<span class="wait">&nbsp;<img src="'+ siteurl +'assets/img/loading.gif" alt="" /></span>');
				},
				complete: function() {
					$('.wait').remove();
				},			
				success: function(json) {
					$.each(json, function(entryIndex, entry){
						if (entry['zone'] != '') {
							if(entry['City_ID'] == 1 && json.length == 1) { //only for Bali
								var html = "";
								<?php //set width using search_stateareapanel2 class for advanced search
								if($this->uri->segment(1) == 'advancedsearch') { ?>
								html += '<div class="search_stateareapanel2">';
								html += '<div id="s_search_areapanel2" class="search_areapanel2">';
								<?php } else {//set width using search_stateareapanel_refine class for refine search ?>
								html += '<div class="search_stateareapanel_refine">';
								html += '<div id="s_search_areapane-refine" class="search_areapane_refine">';
								<?php } ?>
								
								html += '<div class="row-fluid">';
								html += '<div class="location-list span12">';
								html += '<input type=\'checkbox\' value class="formcheck childCheckBoxForSubArea'+ entryIndex +'" onclick="sbRestArea(\'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')" checked="checked">';
								html += '<span style="cursor: default;" onclick="sbRestArea(\'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')">&nbsp;'+ entry['Name'] +'</span>';
								html += '</div>';
								html += '</div>';
			
								html += '<div class="row-fluid">';
								for (i = 0; i < entry['zone'].length; i++) {
									html += '<div class="location-list span4">';
									html += '<input name="chkSubArea[]" type="checkbox" value="' + entry['zone'][i]['Subarea_ID'] + '" id="sbChksS'+ entry['zone'][i]['Subarea_ID'] +'" class="formcheck childCheckBoxForSubArea'+ entryIndex +'" onclick="sbPopulateArea(\'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')">';
									html += '<span style="cursor: default;" class="sbChks" onclick="sbCheckUncheck(\'sbChksS'+ entry['zone'][i]['Subarea_ID'] +'\', \'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')">&nbsp; '+ entry['zone'][i]['Name'] +'</span>';
									html += '</div>';
									if((i+1) % 3 == 0) {
										html += '</div>';
										html += '<div class="row-fluid">';
									}
								}
								html += '</div>';
									
								html += '</div>';
								html += '</div>';
								$('#ddlSubArea').html(html);

								$('#ddlLocation').empty();
								$('#Add_Your_Property_Location').hide();
								$('#Add_Your_Property_SubArea').show();
							} else {
								if(entry['City_ID'] == 1) { //is Bali
									var html = "";
									<?php //set width using search_stateareapanel2 class for advanced search
									if($this->uri->segment(1) == 'advancedsearch') { ?>
									html += '<div class="search_stateareapanel2">';
									html += '<div id="s_search_areapanel2" class="search_areapanel2">';
									<?php } else {//set width using search_stateareapanel_refine class for refine search ?>
									html += '<div class="search_stateareapanel_refine">';
									html += '<div id="s_search_areapane-refine" class="search_areapane_refine">';
									<?php } ?>
									
									html += '<div class="row-fluid">';
									html += '<div class="location-list span12">';
									html += '<input type=\'checkbox\' value class="formcheck childCheckBoxForSubArea'+ entryIndex +'" onclick="sbRestArea(\'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')" checked="checked">';
									html += '<span style="cursor: default;" onclick="sbRestArea(\'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')">&nbsp;'+ entry['Name'] +'</span>';
									html += '</div>';
									html += '</div>';
				
									html += '<div class="row-fluid">';
									for (i = 0; i < entry['zone'].length; i++) {
										html += '<div class="location-list span4">';
										html += '<input name="chkSubArea[]" type="checkbox" value="' + entry['zone'][i]['Subarea_ID'] + '" id="sbChksS'+ entry['zone'][i]['Subarea_ID'] +'" class="formcheck childCheckBoxForSubArea'+ entryIndex +'" onclick="sbPopulateArea(\'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')">';
										html += '<span style="cursor: default;" class="sbChks" onclick="sbCheckUncheck(\'sbChksS'+ entry['zone'][i]['Subarea_ID'] +'\', \'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')">&nbsp; '+ entry['zone'][i]['Name'] +'</span>';
										html += '</div>';
										if((i+1) % 3 == 0) {
											html += '</div>';
											html += '<div class="row-fluid">';
										}
									}
									html += '</div>';
										
									html += '</div>';
									html += '</div>';
									$('#ddlSubArea').html(html);
									$('#Add_Your_Property_SubArea').show();
								} else { //is Jakarta
									var html = "";
									html += '<div class="span12">';
									html += '<label>';
									html += ''+ entry['Name'] +'';
									html += '</label>';
									html += '<label for="all_location" class="all_location">';
									html += '<input type=\'checkbox\' value checked="checked" class="formcheck childCheckBoxForLocaion" onclick="sbRestArea(\'search_location_area_selections2\',\'childCheckBoxForLocaion\',\'<?php echo $text_location_selected; ?>\')">';
									html += '<span style="cursor:default;" onclick="sbRestArea(\'search_location_area_selections2\',\'childCheckBoxForLocaion\',\'<?php echo $text_location_selected; ?>\')">';
									html += '<?php echo $text_all; ?>';
									html += '</span>';
									html += '</label>';
									
									for (i = 0; i < entry['zone'].length; i++) {
										html += '<label for="location_'+ entry['zone'][i]['Location_ID'] +'" class="child_location">';
										html += '<input type=\'checkbox\' value="' + entry['zone'][i]['Location_ID'] + '" class="formcheck childCheckBoxForLocaion" id="sbChksL'+ entry['zone'][i]['Location_ID'] +'" name="locations[]" onclick="sbPopulateArea(\'search_location_area_selections2\',\'childCheckBoxForLocaion\',\'<?php echo $text_location_selected; ?>\')">';
										html += '<span style="cursor:default;" class="sbChks" onclick="sbCheckUncheck(\'sbChksL'+entry['zone'][i]['Location_ID']+'\', \'search_location_area_selections2\',\'childCheckBoxForLocaion\',\'<?php echo $text_location_selected; ?>\')">';
										html += ''+ entry['zone'][i]['Name'] +'';
										html += '</span>';
										html += '</label>';
									}
									
									html += '</div>';
									$('#ddlLocation').html(html);
									$('#Add_Your_Property_Location').show();
								}
							}
						}					
					});
				},
				error: function(xhr, ajaxOptions, thrownError) {
					alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
        } else {
            //reset all selected input tag
            $('#Add_Your_Property_Location').show();
			$('#Add_Your_Property_SubArea').show();
            $('#ddlLocation').empty();
        	$('#ddlSubArea').empty();
        	//hide location frame
        	hideZone("search_locationareapanel");
        	hideZone("search_subareaareapanel");
        }
	}), $('#ddlLocation').bind('click', function(){
		var cityArr = new Array;
		var locationArr = new Array;
		
        cityArr = $('#ddlCity .child_city input:checkbox:checked').map(function() {
            return this.value;
        }).get().join(',');

        locationArr = $('#ddlLocation .child_location input:checkbox:checked').map(function() {
            return this.value;
        }).get().join(',');

        if(cityArr.indexOf(',') == 0) {
        	cityArr = cityArr.replace(',','');
        }

        if(locationArr.indexOf(',') == 0) {
        	locationArr = locationArr.replace(',','');
        }

        if(locationArr.length > 0) {
	        $.ajax({
				url: 'member/getListAllSubArea?locationVal=' + locationArr,
				dataType: 'json',
				beforeSend: function() {
					$('#ddlLocation').after('<span class="wait">&nbsp;<img src="'+ siteurl +'assets/img/loading.gif" alt="" /></span>');
				},
				complete: function() {
					$('.wait').remove();
				},			
				success: function(json) {
					var html = "";
					<?php //set width using search_stateareapanel2 class for advanced search
					if($this->uri->segment(1) == 'advancedsearch') { ?>
					html += '<div class="search_stateareapanel2">';
					html += '<div id="s_search_areapanel2" class="search_areapanel2">';
					<?php } else {//set width using search_stateareapanel_refine class for refine search ?>
					html += '<div class="search_stateareapanel_refine">';
					html += '<div id="s_search_areapane-refine" class="search_areapane_refine">';
					<?php } ?>
					
					$.each(json, function(entryIndex, entry){
						html += '<div class="row-fluid">';
						html += '<div class="location-list span12">';
						html += '<input type=\'checkbox\' value class="formcheck childCheckBoxForSubArea'+ entryIndex +'" onclick="sbRestArea(\'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')" checked="checked">';
						html += '<span style="cursor: default;" onclick="sbRestArea(\'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')">&nbsp;'+ entry['Name'] +'</span>';
						html += '</div>';
						html += '</div>';
	
						html += '<div class="row-fluid">';
						for (i = 0; i < entry['zone'].length; i++) {
							html += '<div class="location-list span4">';
							html += '<input name="chkSubArea[]" type="checkbox" value="' + entry['zone'][i]['Subarea_ID'] + '" id="sbChksS'+ entry['zone'][i]['Subarea_ID'] +'" class="formcheck childCheckBoxForSubArea'+ entryIndex +'" onclick="sbPopulateArea(\'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')">';
							html += '<span style="cursor: default;" class="sbChks" onclick="sbCheckUncheck(\'sbChksS'+ entry['zone'][i]['Subarea_ID'] +'\', \'search_subarea_area_selections2\',\'childCheckBoxForSubArea'+ entryIndex +'\',\''+ entry['Name'] +'\')">&nbsp; '+ entry['zone'][i]['Name'] +'</span>';
							html += '</div>';
							if((i+1) % 3 == 0) {
								html += '</div>';
								html += '<div class="row-fluid">';
							}
						}
						html += '</div>';
					});
					html += '</div>';
					html += '</div>';
					$('#ddlSubArea').html(html);
				},
				error: function(xhr, ajaxOptions, thrownError) {
					alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
        } else {
        	//reset all selected input tag
        	$('#ddlSubArea input:checkbox:checked').attr('checked', false);
        	//hide location frame
        	hideZone("search_subareaareapanel");
        }        
	});
	//multiselect widget master
});
</script>
